package com.yanqu.road.dao.impl.wingroom;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.UserFurnitureSuitData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2022/3/8 18:23
 */
public class UserFurnitureSuitDaoImpl extends TempDataObjectDao<UserFurnitureSuitData> {

    public static String insertSql = "INSERT INTO t_u_wingroom_suit(userId,suitType,suitLv) values(?,?,?)";
    @Override
    protected boolean addData(UserFurnitureSuitData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSuitType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSuitLv()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_wingroom_suit set suitLv=? where userId=? and suitType = ?";

    @Override
    protected boolean updateData(UserFurnitureSuitData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getSuitLv()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSuitType()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserFurnitureSuitData parseData(ResultSet rs) throws SQLException {
        UserFurnitureSuitData data = new UserFurnitureSuitData();
        data.setUserId(rs.getLong("userId"));
        data.setSuitType(rs.getInt("suitType"));
        data.setSuitLv(rs.getInt("suitLv"));
        return data;
    }

    public static String selectSql = "SELECT * FROM t_u_wingroom_suit WHERE userId = ?";

    public Map<Integer, UserFurnitureSuitData> getUserFurnitureSuitMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserFurnitureSuitData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserFurnitureSuitData data = parse(rs);
                dataMap.put(data.getSuitType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
